package com.zzy.database.controller;

import com.zzy.database.pojo.Loan;
import com.zzy.database.pojo.LoanPayment;
import com.zzy.database.pojo.Result;
import com.zzy.database.service.LoanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/loan")
public class LoanController {

    @Autowired
    private LoanService loanService;

    //申请贷款操作
    @PostMapping("/apply")
    public Result applyLoan(@RequestBody Loan loan) {
        System.out.println("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
        boolean success = loanService.applyLoan(loan);
        System.out.println(success+"success");
        if (success == true) {
            System.out.println("8888");
            return Result.success();
        } else {
            return Result.error("贷款失败");
        }
    }

    //还款操作
    @PostMapping("/payment")
    public Result makePayment(@RequestBody LoanPayment loanpayment) {
        System.out.println("勃起");
        boolean success = loanService.makePayment(loanpayment);
        if (success == true) {
            System.out.println("???");
            return Result.success();
        } else {
            return Result.error("还款失败");
        }
    }

    //获取某人贷款信息
    @GetMapping("/status/{loanId}")
    public Result<List<Loan>> getLoanStatus(@PathVariable String loanId) {
        log.info("???????????????????");
        List<Loan>loanreturn=loanService.getLoanStatus(loanId);
        //System.out.println(loanreturn.size());
        if(loanreturn.size()!=0)
        {
            System.out.println("1");
            return new Result<Loan>().success(loanreturn);
        }
        else
        {
            System.out.println("2");
            return Result.error("未查询到贷款信息");
        }
    }
    //获取某贷款还款信息
    @GetMapping("/payment/{loanpayid}")
    public Result<List<LoanPayment>> getLoanPayStatus(@PathVariable String loanpayid)
    {
        List<LoanPayment> loanPaymentreturn=loanService.getLoanPayStatus(loanpayid);
        if(loanPaymentreturn!=null)
        {
            return new Result<LoanPayment>().success(loanPaymentreturn);
        }
        else {
            return Result.error("未查询到还款信息");
        }
    }
}
